from functools import partial

# partial() 固定某些参数并返回一个新的callable 对象。这个新的callable接受未赋值的参数，
# 然后跟之前已经赋值过的参数合并起来，最后将所有参数传递给原始函数。

def spam(a, b, c, d):
    print(a, b, c, d)

s1 = partial(spam, 1)
s1(2, 3, 4)
s1(4, 5, 6)
print()

s2 = partial(spam, d = 42)
s2(1, 2, 3)
s2(4, 5, 5)
print()

s3 = partial(spam, 1, 2, d = 42)
s3(3)
s3(4)
s3(5)
print()

import math
def distance(p1, p2):
    x1, y1 = p1
    x2, y2 = p2
    return math.hypot(x2 - x1, y2 - y1)

points = [ (1, 2), (3, 4), (5, 6), (7, 8) ]
pt = (4, 3)
points.sort(key = partial(distance, pt))
print(points)
